﻿using MyX.Bus;
using MyX.Core;
using MyX.Message;
using MyX.Provider;
using MyX.Store;
using MyX.Work;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyX.Config
{
    public class XConfig
    {
        public XConfig UseCommandRepository(IProvider provider)
        {
            ServiceLocator.Register<XICommandRepository>("command_repository", provider);
            return this;
        }

        public XConfig UseEventRepository(IProvider provider)
        {
            ServiceLocator.Register<XIEventRepository>("event_repository", provider);
            return this;
        }

        public XConfig UseSimpleDefault()
        {
            ServiceLocator.Register<XIMQ>(new MQProvider());
            ServiceLocator.Register<XIMessageBus>("command_bus", new CommandBusProvider(ServiceLocator.Get<XIMQ>()));
            ServiceLocator.Register<XIMessageBus>("event_bus", new EventBusProvider(ServiceLocator.Get<XIMQ>()));
            ServiceLocator.Register<XIMessageBus>("status_bus", new StatusBusProvider(ServiceLocator.Get<XIMQ>()));
            ServiceLocator.Register<XIMessageBus>("result_bus", new ResultBusProvider(ServiceLocator.Get<XIMQ>()));
            ServiceLocator.Register<XIEntityObserver>("send_observer", new EntitySendObserverProvider());
            ServiceLocator.Register<XIEntityObserver>("save_observer", new EntitySaveObserverProvider());

            var storeProvider = new StoreProvider();
            ServiceLocator.Register<XICommandRepository>("command_repository", storeProvider);
            ServiceLocator.Register<XIEventRepository>("event_repository", storeProvider);

            WorkTables.HandleWorks.Add(new XWorkStatusRun<XWorkStatus>(
                x =>
                {
                    XMessageHelper.WorkStatus[x.SourceMessageID] = x;
                }));
            WorkTables.ResultWorks.Add(new XWorkResultRun<XWorkResult>(x => XMessageHelper.WorkResult[x.SourceMessageID] = x));
            return this;
        }


        public static XConfig Use
        {
            get
            {
                if (_config == null)
                    _config = new XConfig();
                return _config;
            }
        }

        private static XConfig _config;
        private XConfig()
        {

        }


    }
}
